/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Comp_02_Checkbox.java]
 * Creation Date        [23-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing.components;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * Swing复选框组件
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Comp_02_Checkbox {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				CheckBoxFrame frame = new CheckBoxFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}

}

/**
 * 复选框窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class CheckBoxFrame extends JFrame {

	private static final long serialVersionUID = -2253994326245949816L;

	private JCheckBox bold = null;
	private JCheckBox italic = null;
	private JPanel checkBoxPanel = null;

	private JLabel mainLabel = null;

	/**
	 * 构造方法
	 * 
	 * @throws HeadlessException
	 */
	public CheckBoxFrame() throws HeadlessException {
		setTitle("Checkbox Test");
		this.setSize(300, 200);
		setLocationByPlatform(true);
		setResizable(false);

		MyActionListener listener = new MyActionListener();

		// 初始化窗体的Center部分
		mainLabel = new JLabel("Today is September 1st", SwingConstants.CENTER);
		mainLabel.setFont(new Font("Serif", Font.PLAIN, 12));

		// 初始化窗体的South部分
		bold = new JCheckBox("Bold");
		// bold.setSelected(true);// 设置为默认选中
		bold.addActionListener(listener);
		italic = new JCheckBox("Italic");
		italic.addActionListener(listener);
		checkBoxPanel = new JPanel();
		checkBoxPanel.add(bold);
		checkBoxPanel.add(italic);

		getContentPane().add(mainLabel, BorderLayout.CENTER);
		getContentPane().add(checkBoxPanel, BorderLayout.SOUTH);

	}

	/**
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			int fontMode = 0;
			if (bold.isSelected()) { // 粗体
				fontMode += Font.BOLD;
			}
			if (italic.isSelected()) { // 斜体
				fontMode += Font.ITALIC;
			}
			mainLabel.setFont(new Font("Serif", fontMode, 12));
		}
	}

}
